package com.stone.persistent.recyclerview.library;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.view.NestedScrollingParent3;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;


public class ParentRecyclerView extends BaseRecyclerView implements NestedScrollingParent3 {
    private View childPagerContainer;
    private ViewPager innerViewPager;
    private ViewPager2 innerViewPager2;
    private boolean doNotInterceptTouchEvent;
    private int stickyHeight;

    public boolean onInterceptTouchEvent(@Nullable MotionEvent e) {

        if (e.getAction() == 0) {
            ChildRecyclerViews childRecyclerView = this.findCurrentChildRecyclerView();
            this.doNotInterceptTouchEvent = this.doNotInterceptTouch(e.getRawY(), childRecyclerView);
            this.stopFling();
            if (childRecyclerView != null) {
                childRecyclerView.stopFling();
            }
        }

        return this.doNotInterceptTouchEvent ? false : super.onInterceptTouchEvent(e);
    }

    private final boolean doNotInterceptTouch(float rawY, ChildRecyclerViews childRecyclerView) {
        if (childRecyclerView != null && this.childPagerContainer != null) {
            int[] coorValue = new int[2];
            childRecyclerView.getLocationOnScreen(coorValue);
            int childRecyclerViewY = coorValue[1];
            if (rawY > (float) childRecyclerViewY) {
                return true;
            }
            View var10000 = this.childPagerContainer;

        }

        return false;
    }

    public void onNestedPreScroll(@NotNull View target, int dx, int dy, @NotNull int[] consumed, int type) {
        if (target instanceof ChildRecyclerViews) {
            int consumeY = dy;
            int childScrollY = ((ChildRecyclerViews) target).computeVerticalScrollOffset();
            View var10000 = this.childPagerContainer;


            if (var10000.getTop() > this.stickyHeight) {
                if (childScrollY > 0 && dy < 0) {
                    consumeY = 0;
                } else {
                    var10000 = this.childPagerContainer;


                    if (var10000.getTop() - dy < this.stickyHeight) {
                        var10000 = this.childPagerContainer;
                        consumeY = var10000.getTop() - this.stickyHeight;
                    }
                }
            } else {
                var10000 = this.childPagerContainer;
                if (var10000.getTop() == this.stickyHeight) {
                    consumeY = -dy < childScrollY ? 0 : dy + childScrollY;
                }
            }

            if (consumeY != 0) {
                consumed[1] = consumeY;
                this.scrollBy(0, consumeY);
            }
        }

    }

    public void onScrollStateChanged(int state) {
        if (state == 0) {
            int velocityY = this.getVelocityY();
            if (velocityY > 0) {
                ChildRecyclerViews childRecyclerView = this.findCurrentChildRecyclerView();
                if (childRecyclerView != null) {
                    childRecyclerView.fling(0, velocityY);
                }
            }
        }

    }

    public boolean onStartNestedScroll(@NotNull View child, @NotNull View target, int axes, int type) {
        return true;
    }

    public void onNestedScroll(@NotNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    }

    public void onNestedScroll(@NotNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NotNull int[] consumed) {
    }

    public void onNestedScrollAccepted(@NotNull View child, @NotNull View target, int axes, int type) {
    }

    public void onStopNestedScroll(@NotNull View target, int type) {
    }

    private final ChildRecyclerViews findCurrentChildRecyclerView() {
        Object var14 = null;
        if (this.innerViewPager != null) {
            ViewPager viewPager = this.innerViewPager;
            int currentItem = viewPager.getCurrentItem();
            int i = 0;
            viewPager = this.innerViewPager;
            for (int var3 = viewPager.getChildCount(); i < var3; ++i) {
                View itemChildView = viewPager.getChildAt(i);
                ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) itemChildView.getLayoutParams();

                Field positionField = null;
                try {
                    positionField = layoutParams.getClass().getDeclaredField("position");
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
                positionField.setAccessible(true);

                try {
                    var14 = positionField.get(layoutParams);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }


                int position = (Integer) var14;

                if (!layoutParams.isDecor && currentItem == position) {
                    if (itemChildView instanceof ChildRecyclerViews) {
                        return (ChildRecyclerViews) itemChildView;
                    }

                    Object tagView = itemChildView.getTag(R.id.tag_saved_child_recycler_view);
                    if (tagView instanceof ChildRecyclerViews) {
                        return (ChildRecyclerViews) tagView;
                    }
                }
            }
        } else if (this.innerViewPager2 != null) {

            Field layoutManagerFiled = null;
            try {
                layoutManagerFiled = ViewPager2.class.getDeclaredField("mLayoutManager");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            layoutManagerFiled.setAccessible(true);
            try {
                var14 = layoutManagerFiled.get(this.innerViewPager2);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }


            LinearLayoutManager pagerLayoutManager = (LinearLayoutManager) var14;
            ViewPager2 var10001 = this.innerViewPager2;


            View currentChild = pagerLayoutManager.findViewByPosition(var10001.getCurrentItem());
            if (currentChild instanceof ChildRecyclerViews) {
                return (ChildRecyclerViews) currentChild;
            }

            Object tagView = currentChild != null ? currentChild.getTag(R.id.tag_saved_child_recycler_view) : null;
            if (tagView instanceof ChildRecyclerViews) {
                return (ChildRecyclerViews) tagView;
            }
        }

        return null;
    }

    public final void setInnerViewPager(@Nullable ViewPager viewPager) {
        this.innerViewPager = viewPager;
    }

    public final void setInnerViewPager2(@Nullable ViewPager2 viewPager2) {
        this.innerViewPager2 = viewPager2;
    }

    public final void setChildPagerContainer(@NotNull View childPagerContainer) {

        if (childPagerContainer != null) {
            this.childPagerContainer = childPagerContainer;
            this.post( new Runnable() {
                public final void run() {
                    ParentRecyclerView.this.adjustChildPagerContainerHeight();
                }
            });
        }

    }

    public final void setStickyHeight(int stickyHeight) {
        final int scrollOffset = this.stickyHeight - stickyHeight;
        this.stickyHeight = stickyHeight;
        this.adjustChildPagerContainerHeight();
        this.post(new Runnable() {
            public final void run() {
                ParentRecyclerView.this.scrollBy(0, scrollOffset);
            }
        });
    }

    private final void adjustChildPagerContainerHeight() {
        if (this.childPagerContainer != null) {
            View view = this.childPagerContainer;
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            int newHeight = this.getHeight() - this.stickyHeight;
            if (newHeight != layoutParams.height) {
                layoutParams.height = newHeight;
                view = this.childPagerContainer;
                view.setLayoutParams(layoutParams);
            }
        }

    }

    public ParentRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOverScrollMode(View.OVER_SCROLL_NEVER);
        this.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    }


    public ParentRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParentRecyclerView(@NotNull Context context) {
        this(context, null);
    }


}